//
//  YLLOrginalView.swift
//  YLLSina
//
//  Created by 于玲玲 on 16/8/1.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit
let HomeCellMargin = 10

class YLLOrginalView: UIView {

    var constarit: Constraint?
    
    var orginalViewModel: YLLHomeCellViewModel?{
        
        didSet{
            
            nameLabel.text = orginalViewModel?.screen_name
            
            //设置头像
            photoImageView.sd_setImageWithURL(orginalViewModel?.photoUrl, placeholderImage: UIImage(named: "avatar_default_big"))
            
            verfitiedImageView.image = orginalViewModel?.verfited_Image
            
            levelImageView.image = orginalViewModel?.level_Image
            
            contentLabel.text = orginalViewModel?.statusModel?.text
            
            sourceLabel.text = orginalViewModel?.source
//            print(sourceLabel.text)
            
            constarit?.uninstall()
            
            if let pic_urls = orginalViewModel?.statusModel?.pic_urls where pic_urls.count > 0{
                
                pictureView.hidden = false
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.constarit = make.bottom.equalTo(pictureView.snp_bottom).constraint
                })
                
                
                
            }
            
        }
    }
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI(){
        
        addSubview(photoImageView)
        addSubview(nameLabel)
        addSubview(verfitiedImageView)
        addSubview(levelImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        // 添加约束
        photoImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(HomeCellMargin)
            make.top.equalTo(self.snp_top).offset(HomeCellMargin)
            
            make.width.height.equalTo(50)
        }

         verfitiedImageView.snp_makeConstraints { (make) -> Void in
        make.centerX.equalTo(photoImageView.snp_right)
        make.centerY.equalTo(photoImageView.snp_bottom)
        
    }
    
    nameLabel.snp_makeConstraints { (make) -> Void in
    make.left.equalTo(photoImageView.snp_right).offset(HomeCellMargin)
    make.top.equalTo(photoImageView.snp_top)
    }
    
    levelImageView.snp_makeConstraints { (make) -> Void in
    make.left.equalTo(nameLabel.snp_right).offset(HomeCellMargin)
    make.centerY.equalTo(nameLabel.snp_centerY)
    }
    
    timeLabel.snp_makeConstraints { (make) -> Void in
    make.left.equalTo(photoImageView.snp_right).offset(HomeCellMargin)
    make.top.equalTo(nameLabel.snp_bottom).offset(HomeCellMargin)
    }
    
    sourceLabel.snp_makeConstraints { (make) -> Void in
    make.centerY.equalTo(timeLabel.snp_centerY)
    make.left.equalTo(timeLabel.snp_right).offset(HomeCellMargin)
    }
    
    contentLabel.snp_makeConstraints { (make) -> Void in
    
    make.left.equalTo(self.snp_left)
    make.top.equalTo(photoImageView.snp_bottom).offset(HomeCellMargin)
    make.right.equalTo(self.snp_right)
    make.bottom.equalTo(self.snp_bottom)
    
    }
        // 配图的约束该怎么添加? 按照转发微博的实现思路来
        /*
         1. 我们认为所有的微博都有配图
         2. 我们再根据数据,没有微博配图的,让微博配图隐藏或者高度=0
         
         */

        pictureView.snp_makeConstraints { (make) in
            make.left.equalTo(self.snp_left).offset(HomeCellMargin)
            make.top.equalTo(contentLabel.snp_bottom)
            
            // 宽和高的约束,我们是不是不知道?
            // 我们为了验证思路,可以先随便给一个
//            make.width.equalTo(100)
//            make.height.equalTo(100)

        }
        
        

    }

    private lazy var photoImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "avatar_default_big")
        
        return img
        
    }()
    
    /// 认证图片
    private lazy var verfitiedImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "avatar_enterprise_vip")
        
        return img
    }()
    
    private lazy var nameLabel: UILabel = {
        
        let label = UILabel()
        
        label.text = "名字"
        label.font = UIFont.systemFontOfSize(15)
        label.textColor = UIColor.darkGrayColor()
        
        
        
        return label
    }()
    
    /// 等级
    private lazy var levelImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "common_icon_membership_level1")
        
        return img
    }()
    
    ///时间
    private lazy var timeLabel: UILabel = {
        
        let label = UILabel()
        
        label.text = "时间"
        label.font = UIFont.systemFontOfSize(13)
        label.textColor = UIColor.orangeColor()
        
        
        return label
    }()
    
    ///来源
    private lazy var sourceLabel: UILabel = {
        
        let label = UILabel()
        
        label.text = "来源"
        label.font = UIFont.systemFontOfSize(13)
        label.textColor = UIColor.darkGrayColor()
        
        return label
    }()
    
    ///内容
    private lazy var contentLabel: UILabel = {
        
        let label = UILabel()
        
        label.text = "文件而反过来看就按了沙发了开始进而发生看来减肥"
        label.font = UIFont.systemFontOfSize(16)
        label.textColor = UIColor.blackColor()
        
        label.numberOfLines = 0
        
        return label
    }()

    private lazy var pictureView: YLLPhotoView = YLLPhotoView()
}
